package crud;

import org.hibernate.Session;
import org.junit.Test;

import Domain.News;
import utils.HibernateUtils;

public class TestSelect {

	
	@Test
	public void testGET() {
		Session session = HibernateUtils.getSession();
		try {
			session.beginTransaction();
			
			//get 查不到返回null
			News news = session.get(News.class, 1);
			System.out.println(news);
			
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
	
	@Test
	public void testLoad() {
		Session session = HibernateUtils.getSession();
		try {
			session.beginTransaction();
			
			//load 查不到返回异常
			News news = session.load(News.class, 1);
			System.out.println(news);
			
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
	
	@Test
	public void testGET2() {
		Session session = HibernateUtils.getSession();
		try {
			//必须在事物下查询
			News news = session.get(News.class, 1);
			System.out.println(news);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test//curd 执行顺序
	public void testSequence() {
		Session session = HibernateUtils.getSession();
		try {
			session.beginTransaction();
			//删除
			News news = session.get(News.class, 3);
			
			session.delete(news);
			session.flush();
			//修改
			News news2 = session.get(News.class, 4);
			news2.setAuthor("新华社");
			session.update(news2);
			//添加
			session.save(new News("小米倒闭", "湖北新闻网"));
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
	/**
	 * 无flush隔断：先两次查询，增加,修改,删除
	 * 有flush隔断：查询，删除，查询，增加
	 */
}
